package org.example.myleet.p682;

import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    public int calPoints(String[] ops) {
        Deque<Integer> stack = new LinkedList<>();
        for (String op : ops) {
            if (Character.isDigit(op.charAt(op.length() - 1))) {
                stack.push(Integer.valueOf(op));
            } else if (op.charAt(0) == '+') {
                int prev = stack.pop();
                int prevPrev = stack.peek();
                stack.push(prev);
                stack.push(prevPrev + prev);
            } else if (op.charAt(0) == 'D') {
                int prev = stack.peek();
                stack.push(prev * 2);
            } else if (op.charAt(0) == 'C') {
                stack.pop();
            }
        }
        int sum = 0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }
}
